package psrc.bd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import psrc.logic.util.beans.EmpresaBean;

public class EmpresaDAO extends AbstractDAO {

	public EmpresaDAO(Connection conn) {
		super(conn);
	}

	@Override
	public <T> void adiciona(T _bean) throws SQLException{
		EmpresaBean bean = (EmpresaBean) _bean;
		String sql = "insert into Empresa "
				+ "(razao_social, nome_fantasia, cnpj, inscricao_estadual, telefone_1, telefone_2, email) values (?, ?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);

			stmt.setString(1, bean.getRazaoSocial());
			stmt.setString(2, bean.getNomeFantasia());
			stmt.setString(3, bean.getCnpj());
			stmt.setString(4, bean.getInscricaoEstadual());
			stmt.setString(5, bean.getTelefone1());
			stmt.setString(6, bean.getTelefone2());
			stmt.setString(7, bean.getEmail());
			
			stmt.execute();
			stmt.close();

		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> void remove(T _bean) throws SQLException{
		EmpresaBean bean = (EmpresaBean) _bean;
		try {
			PreparedStatement stmt = connection.prepareStatement("delete from Empresa where id_empresa=?");

			stmt.setInt(1, Integer.parseInt(bean.getIdEmpresa()));

			stmt.execute();
			stmt.close();

		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> void edita(T _bean) throws SQLException{
		EmpresaBean bean = (EmpresaBean) _bean;

		String sql = "update Empresa set " +"razao_social=?, nome_fantasia=?, cnpj=?, inscricao_estadual=?, telefone_1=?, telefone_2=?, email=? where empresa=?";
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);

			stmt.setString(1, bean.getRazaoSocial());
			stmt.setString(2, bean.getNomeFantasia());
			stmt.setString(3, bean.getCnpj());
			stmt.setString(4, bean.getInscricaoEstadual());
			stmt.setString(5, bean.getTelefone1());
			stmt.setString(6, bean.getTelefone2());
			stmt.setString(7, bean.getEmail());
			stmt.setInt(8, Integer.parseInt(bean.getIdEmpresa()));
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> List<T> seleciona() throws SQLException{
		List<EmpresaBean> empresas = new ArrayList<EmpresaBean>();

		try {
			PreparedStatement stmt = connection.prepareStatement("select * from Empresa");

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				EmpresaBean bean = new EmpresaBean();
				bean.setRazaoSocial(rs.getString("razao_social"));
				bean.setNomeFantasia(rs.getString("nome_fantasia"));
				bean.setCnpj(rs.getString("cnpj"));
				bean.setInscricaoEstadual(rs.getString("inscricao_estadual"));
				bean.setTelefone1(rs.getString("telefone_1"));
				bean.setTelefone2(rs.getString("telefone_2"));
				bean.setEmail(rs.getString("email"));
				bean.setIdEmpresa(rs.getString("id_empresa"));

				empresas.add(bean);
			}
			rs.close();
			stmt.close();
			return (List<T>) empresas;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

@Override
	public <T> List<T> selecionaPorId(T param) throws SQLException {
		List<EmpresaBean> empresas = new ArrayList<EmpresaBean>();
		EmpresaBean user = (EmpresaBean) param;
		try {
			PreparedStatement stmt = connection
					.prepareStatement("select * from Empresa where id_empresa=?");
			stmt.setString(1, user.getIdEmpresa());
			ResultSet rs = stmt.executeQuery();
			
			while (rs.next()) {
				EmpresaBean bean = new EmpresaBean();
				bean.setRazaoSocial(rs.getString("razao_social"));
				bean.setNomeFantasia(rs.getString("nome_fantasia"));
				bean.setCnpj(rs.getString("cnpj"));
				bean.setInscricaoEstadual(rs.getString("inscricao_estadual"));
				bean.setTelefone1(rs.getString("telefone_1"));
				bean.setTelefone2(rs.getString("telefone_2"));
				bean.setEmail(rs.getString("email"));
				bean.setIdEmpresa(rs.getString("id_empresa"));

				empresas.add(bean);
			}
			rs.close();
			stmt.close();
			return (List<T>) empresas;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}
}
